package com.demo.project.jyfx.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.demo.framework.aspectj.lang.annotation.Log;
import com.demo.framework.aspectj.lang.enums.BusinessType;
import com.demo.project.jyfx.domain.Student;
import com.demo.project.jyfx.service.IStudentService;
import com.demo.framework.web.controller.BaseController;
import com.demo.framework.web.domain.AjaxResult;
import com.demo.common.utils.poi.ExcelUtil;
import com.demo.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;
/**
 * 学生信息Controller
 *
 * @author khks
 * @date 2025-05-29
 */
@RestController
@RequestMapping("/jyfx/student")
public class StudentController extends BaseController
{
    @Autowired
    private IStudentService studentService;

/**
 * 查询学生信息列表
 */
@PreAuthorize("@ss.hasPermi('jyfx:student:list')")
@GetMapping("/list")
    public TableDataInfo list(Student student)
    {
        startPage();
        List<Student> list = studentService.selectStudentList(student);
        return getDataTable(list);
    }

    /**
     * 导出学生信息列表
     */
    @PreAuthorize("@ss.hasPermi('jyfx:student:export')")
    @Log(title = "学生信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Student student)
    {
        List<Student> list = studentService.selectStudentList(student);
        ExcelUtil<Student> util = new ExcelUtil<Student>(Student.class);
        util.exportExcel(response, list, "学生信息数据");
    }

    /**
     * 获取学生信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('jyfx:student:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(studentService.selectStudentById(id));
    }

    /**
     * 新增学生信息
     */
    @PreAuthorize("@ss.hasPermi('jyfx:student:add')")
    @Log(title = "学生信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Student student)
    {
        String operName = getUsername();
        String msg = studentService.insertStudent(student);
        if (msg.length() > 0){
            return error(msg);
        }
        return success("新增成功");
    }

    /**
     * 导入学生信息
     */
    @PreAuthorize("@ss.hasPermi('jyfx:student:import')")
    @Log(title = "学生信息", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<Student> util = new ExcelUtil<Student>(Student.class);
        List<Student> studentList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String msg = studentService.insertStudents(studentList, operName);
        if (msg.length() > 0){
            return error(msg);
        }
        return success("导入成功");
    }

    /**
     * 修改学生信息
     */
    @PreAuthorize("@ss.hasPermi('jyfx:student:edit')")
    @Log(title = "学生信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Student student)
    {
        String operName = getUsername();
        String msg = studentService.updateStudent(student);
        if (msg.length() > 0){
            return error(msg);
        }
        return success("修改成功");
    }

    /**
     * 删除学生信息
     */
    @PreAuthorize("@ss.hasPermi('jyfx:student:remove')")
    @Log(title = "学生信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        String msg = studentService.deleteStudentByIds(ids);
        if (msg.length() > 0){
            return error(msg);
        }
        return success("删除成功");
    }

    /**
     * 学生信息模板下载
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Student> util = new ExcelUtil<>(Student.class);
        util.importTemplateExcel(response, "学生信息数据");
    }
}
